package cn.lll.vuehouse.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.dto.CustomerPageDTO;
import cn.lll.vuehouse.entity.Customer;
import cn.lll.vuehouse.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author: Cong
 * @date: 2023/9/4
 * @version: 1.0
 * @description:
 */

import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.entity.Customer;
import cn.lll.vuehouse.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    @GetMapping("page")
    public ResponseResult getPageList(CustomerPageDTO customerPageDTO) {
        System.out.println("customerPageDTO = " + customerPageDTO);
        return customerService.getCustomerPage(customerPageDTO);
    }

    // getById 编辑客户时回显
    @GetMapping("getById")
    public ResponseResult getCustomerInfo(Integer id) {
        return customerService.getCustomerById(id);
    }

    // 修改客户状态 updateAvailaable
    @PutMapping("updateAvailable")
    public ResponseResult updateState(@RequestBody Customer customer) {
        customerService.updateById(customer);
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        return responseResult;
    }

    // 添加和编辑请求
    @PostMapping("addOrUpdate")
    public ResponseResult addOrUpdateUser(@RequestBody Customer customer) {
        if (ObjectUtil.isNotNull(customer.getId())) {
            return customerService.updateUser(customer);
        }
        return customerService.addUser(customer);
    }

    // 批量删除用户数据
    @DeleteMapping("batchDelete")
    public ResponseResult batchDelete(@RequestBody Integer[] ids) {
        ResponseResult responseResult = new ResponseResult();
        List<Integer> list = Arrays.asList(ids);
        customerService.removeBatchByIds(list);
        responseResult.success();
        return responseResult;
    }


    @GetMapping("lecturer")
    public ResponseResult lecturer(){
        List<Customer> list = customerService.list();
        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS,list);
        return responseResult;
    }

}
